package com.entrogy.dao;

import com.alibaba.fastjson.JSONObject;
import com.entrogy.bean.PayOrderEntity;
import com.entrogy.bean.StudentBasicInfoEntity;
import com.entrogy.bean.StudentEntity;
import com.entrogy.bean.StudentIcEntity;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 *    学生信息dao接口
 */
@Repository
public interface StudentMapper {
	/**
	 *
	 * @param map
	 * @return  分页条件查询学生信息
	 */
	List<Map<String,Object>> getTotalStudentList(Map<String,Object> map);

	/**
	 *
	 * @param map
	 * @return  分页条件查询学生信息  获取总条数
	 */
	List<Map<String,Object>> getTotal(Map<String,Object> map);


	/**
	 *
	 * @param studentEntity
	 * @return 新增学生
	 */
	int insertStudent( StudentEntity studentEntity);

	/**
	 * 修改学生信息
	 * @return
	 */
	int  updateStudent(StudentEntity studentEntity);

	/**
	 *   修改父母id
	 * @param map
	 * @return
	 */
	int  updateStuent1(Map<String,Object> map);

	/**
	 *  根据id 查询学生信息
	 * @param id
	 * @return
	 */
	StudentEntity getStudentById(@Param("id") int id);

	@Select("select * from student_information where grade_id = #{gradeId} and school_code=#{schoolCode}")
	List<StudentEntity> getStudentByGrade(@Param("gradeId") String gradeId, @Param("schoolCode") Integer schoolCode);

	@Update("update student_information set allow_time = #{allowTime} where id = #{id}")
	void updateStudentControl(@Param("allowTime") String allowTime, @Param("id") int id);

	@Update("UPDATE student_information SET leave_type = #{leaveType} where id = #{id}")
	void updateStudentSetUp(@Param("leaveType") int leaveType, @Param("id") int id);

	@Insert("insert into leaveschool_set(uid) value (#{userId})")
	void insertLeaveSchoolSet(@Param("userId") String userId);

    @Update("update leaveschool_set set update_time = now() where uid = #{userId}")
	void updateLeaveSchoolSet(@Param("userId") String userId);

	/**
	 * 根据父母id查询子女id
	 * @param uId
	 * @return
	 */
	List<Integer> findStuIdsByParentId(String uId);

	/**
	 * 根据学生id查询班级id
	 * @param stuId
	 * @return
	 */
	Integer findClassIdByStuId(Integer stuId);

	/**
	 * 查找一个班级的所有学生
	 * @param classId
	 * @param schoolCode
	 * @return
	 */
    List<StudentEntity> listClassStu(@Param(value = "classId") String classId, @Param(value = "schoolCode") Integer schoolCode);

	/**
	 * 通过实义德id查询学生
	 * @param thdid
	 * @return
	 */
	StudentBasicInfoEntity getStudentByThdid(@Param("thdid") String thdid, @Param("schoolCode") Integer schoolCode);

	@Select("select s.student_name,c.course_name as courseName from student_information s,course_information c  s.class_id = c.class_id and s.school_code = c.school_code and s.id = #{studentId}")
	StudentEntity getStudentAndCourseNameById(@Param("studentId") int studentId);

	/**
	 * 根据班级ID和学生姓名查询学生
	 * @param name
	 * @return
	 */
	@Select("select id,student_thdid,student_name,course_id,school_code from student_information where name like '%#{name}%' and school_code = #{schoolCode} and course_id = #{courseId}")
    StudentEntity getStudentByNameAndUid(@Param("courseId") String courseId, @Param("name") String name,@Param("schoolCode") int schoolCode);

	/**
	 *根据年级ID和学生姓名查询学生
	 * @param name
	 * @return
	 */
	@Select("select id,student_thdid,student_name,course_id,school_code from student_information where name like '%#{name}%' and course_id like '%#{gradeId}%' ")
	StudentEntity getStudentByUidAndGradeid(@Param("gradeId") String gradeId, @Param("name") String name,@Param("schoolCode") int schoolCode);
    @Select("select c.course_name from student_information s,course_name c where s.course_id = c.class_id and s.school_code = c.school_code and s.student_thdid = #{thdid} and s.school_code = #{schoolCode}")
	String getCourseNameByStudentThdid(@Param("thdid") String stuTHDid,@Param("schoolCode") int schoolCode);

	/**
	 * 根据班级ID查询出其所有的学生总数
	 * @param classId
	 * @return
	 */
	@Select("select count(*) from student_information wehre course_id = #{classId} and school_code = #{schoolCode}")
	int getStudentCountByClassID(@Param("classId") String classId,@Param("schoolCode") int schoolCode);

	/**
	 * 根据班级ID查询出该班级的进校人数
	 * @param tableName
	 * @param type
	 * @param courseid
	 * @param schoolCode
	 * @return
	 */
	@Select("select count(*) from ${tableName},student_information s where t.person_id=s.student_thdid and s.course_id=#{classId} and t.flag=#{type}")
	int getIntoSchoolCount(@Param("tableName") String tableName, @Param("type") int type,@Param("classId") String courseid, @Param("schoolCode") int schoolCode);

	/**
	 * 根据学校id以及班级id查看某个学生是否存在
	 * @param schoolCode
	 * @param classId
	 * @param studentName
	 * @return
	 */
	@Select("select si.id from student_information si where si.school_code=#{schoolCode} and si.course_id=#{classId} and si.student_name=#{studentName}")
	Integer getStudentBySchoolCodeAndClassId(@Param(value = "schoolCode") Integer schoolCode, @Param(value = "classId") Integer classId, @Param(value = "studentName") String studentName);

	/**
	 * 班主任查看该班所有学生
	 * @param uid
	 * @return
	 */
	List<JSONObject> listStudentSimpleInformation(String uid);

	/**
	 * 根据老师id和家长id去确定一个学生id
	 * @param uid
	 * @param teacherId
	 * @return
	 */
	Integer getStudentIdByParentIdAndTeacherId(@Param(value = "uid") String uid, @Param(value = "teacherId") String teacherId);

	/**
	 * 根据学生姓名和班级id查询学生id
	 * @param studentName
	 * @param classId
	 * @param schoolCode
	 * @return
	 */
    List<Integer> getStudentId(@Param(value = "studentName") String studentName, @Param(value = "classId") Integer classId, @Param(value = "schoolCode") Integer schoolCode);

	/**
	 * 查询学生列表带宿舍信息
	 * @param gradeId
	 * @param classId
	 * @param schoolCode
	 * @return
	 */
    List<JSONObject> listFloorStudent(@Param(value = "gradeId") Integer gradeId, @Param(value = "classId") Integer classId, @Param(value = "schoolCode") Integer schoolCode);

	/**
	 * 查询这些学生的实义德id用于推送通行权限
	 * @param studentIdList
	 * @return
	 */
	List<String> listStudentThdid(@Param(value = "studentIdList") List<Integer> studentIdList);

	/**
	 * 查询该校所有学生的所有信息
	 * @param schoolCode
	 * @param gradeId
	 * @param classId
	 * @param studentId
     * @param studentName
	 * @param icCardNumber
	 * @param allowTime
     * @return
	 */
    List<StudentEntity> listSchoolAllStudent(@Param(value = "schoolCode") Integer schoolCode, @Param(value = "gradeId") Integer gradeId, @Param(value = "classId") Integer classId, @Param(value = "studentId") Integer studentId, @Param("studentName") String studentName, @Param("icCardNumber") String icCardNumber, @Param("allowTime") String allowTime);

	/**
	 * 根据学生id更新实义德id
	 * @param studentId
	 * @param thdId
	 * @return
	 */
	boolean updateStudentThdid(@Param(value = "studentId") Integer studentId, @Param(value = "thdId") int thdId);

	/**
	 * 查询学生的学校id
	 * @param studentId
	 * @return
	 */
    Integer getStudentSchoolCode(Integer studentId);

	/**
	 * 查询班级的学生
	 * @param classId
	 * @param schoolCode
	 * @return
	 */
	List<StudentEntity> listStudentByClassId(@Param(value = "classId") String classId, @Param(value = "schoolCode") Integer schoolCode);

	/**
	 * 更新学生的楼层
	 * @param floorId
	 * @param studentId
	 * @return
	 */
	int updateStudentFloorId(@Param("floorId") Integer floorId, @Param("studentId") int studentId);

	/**
	 * 初始化学生基本信息
	 * @param schoolCode
	 * @param gradeId
	 * @param classId
	 * @param studentId
	 * @return
	 */
	int updateStudentBasicData(@Param(value = "schoolCode") Integer schoolCode, @Param(value = "gradeId") Integer gradeId, @Param(value = "classId") Integer classId, @Param(value = "studentId") Integer studentId);

	/**
	 * 初始化学生性别信息
	 * @param schoolCode
	 * @param gradeId
	 * @param classId
	 * @param studentId
	 * @return
	 */
	int updateStudentSexData(@Param(value = "schoolCode") Integer schoolCode, @Param(value = "gradeId") Integer gradeId, @Param(value = "classId") Integer classId, @Param(value = "studentId") Integer studentId);

	/**
	 * 初始化学生离校信息
	 * @param schoolCode
	 * @param gradeId
	 * @param classId
	 * @param studentId
	 * @return
	 */
	int updateStudentLeaveTypeData(@Param(value = "schoolCode") Integer schoolCode, @Param(value = "gradeId") Integer gradeId, @Param(value = "classId") Integer classId, @Param(value = "studentId") Integer studentId);

	/**
	 * 删除学生
	 * @param schoolCode
	 * @param gradeId
	 * @param classId
	 * @param studentId
	 * @return
	 */
    int deleteStudent(@Param(value = "schoolCode") Integer schoolCode, @Param(value = "gradeId") Integer gradeId, @Param(value = "classId") Integer classId, @Param(value = "studentId") Integer studentId);

	/**
	 * 查询学生（临时使用）
	 * @return
	 */
	List<StudentEntity> listSchoolStudent();

	List<StudentEntity> listInitStudent();

	void updateStudentParent(@Param("id") int id, @Param("parent1Id") String parent1Id, @Param("parent2Id") String parent2Id);

	/**
	 * 统计学生数量
	 * @param schoolCode
	 * @param gradeId
	 * @param courseId
	 * @param studentName
	 * @return
	 */
	Integer countStudent(@Param("schoolCode") Integer schoolCode, @Param("gradeId") Integer gradeId, @Param("courseId") Integer courseId, @Param("studentName") String studentName);

	/**
	 * 查询学生的通行时段id
	 * @param id
	 * @param cycleTableName
	 * @return
	 */
	Integer getStudentCycleId(@Param("studentId") int id, @Param("cycleTableName") String cycleTableName);

	/**
	 *	更新学生信息
	 * @param studentId
	 * @param studentPhotoUrl
	 * @return
	 */
    Integer updateStudentInfo(@Param("studentId") Integer studentId, @Param("studentPhotoUrl") String studentPhotoUrl);

	/**
	 * 得到某些学生的支付状态
	 * @param studentList
	 * @return
	 */
	int countStudentPayStatus(@Param("studentList") List<StudentBasicInfoEntity> studentList);

    /**
     * 得到某个学生的支付状态
     * @param studentId
     * @return
     */
    PayOrderEntity getStudentPayStatus(@Param("studentId") Integer studentId);

	/**
	 * 查询学生
	 * @param schoolCode
	 * @param isBind
	 * @param gradeId
	 * @param classId
	 * @param studentName
	 * @return
	 */
	List<StudentIcEntity> listStudentForIcManager(@Param("schoolCode") Integer schoolCode, @Param("isBind") Integer isBind, @Param("gradeId") Integer gradeId, @Param("classId") Integer classId, @Param("studentName") String studentName);

	/**
	 * 统计学生人数
	 * @param schoolCode
	 * @param isBind
	 * @param gradeId
	 * @param classId
	 * @param studentName
	 * @return
	 */
	int countStudentForIcManager(@Param("schoolCode") Integer schoolCode, @Param("isBind") Integer isBind, @Param("gradeId") Integer gradeId, @Param("classId") Integer classId, @Param("studentName") String studentName);

	/**
	 * 查询属于该线路的的学生
	 * @param busLineId
	 * @param studentName
	 * @param schoolCode
	 * @return
	 */
	List<StudentBasicInfoEntity> listStudentForBus(@Param("busLineId") Integer busLineId, @Param("studentName") String studentName, @Param("schoolCode") Integer schoolCode);

	/**
	 * 查询属于该线路的的学生人数
	 * @param busLineId
	 * @param studentName
	 * @param schoolCode
	 * @return
	 */
	Integer countStudentForBus(@Param("busLineId") Integer busLineId, @Param("studentName") String studentName, @Param("schoolCode") Integer schoolCode);

	/**
	 * 查询学生的家长微信id
	 * @param studentIdList
	 * @return
	 */
	List<JSONObject> listParentOpenId(@Param("studentIdList") List<Integer> studentIdList);

	/**
	 * 查询学生
	 * @param schoolCode
	 * @param gradeId
	 * @param classId
	 * @return
	 */
    List<StudentBasicInfoEntity> listStudent(@Param("schoolCode") Integer schoolCode, @Param("gradeId") Integer gradeId, @Param("classId") Integer classId, @Param("studentId") Integer studentId, @Param("studentName") String studentName);

	/**
	 * 查询学生家长登录数
	 * @param studentId
	 * @return
	 */
	int countParentLogin(@Param("studentId") Integer studentId);

	/**
	 * 查询学生家长登录数（按学生去重）
	 * @param schoolCode
	 * @param gradeId
	 * @param classId
	 * @return
	 */
    int countStudentParentLogin(@Param("schoolCode") Integer schoolCode, @Param("gradeId") Integer gradeId, @Param("classId") Integer classId);
}
